package com.fbinggun.dt.task.conditions;

import com.fbinggun.dt.task.Task;
import com.fbinggun.fbinggunutil.ItemUtil;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CraftItem extends Conditions{
    List<String> list = new ArrayList<>();
    public CraftItem(String id, ConfigurationSection config, Task task) {
        super(id, config, task);
        list=config.getStringList("Type");
    }

    @EventHandler
    public void craft(CraftItemEvent event) throws SQLException, ClassNotFoundException {
        if(event.getWhoClicked() instanceof Player) {
            Player player = (Player) event.getWhoClicked();
            if(isRun(player)){
                ItemStack itemStack =event.getRecipe().getResult();
                String itemType = ItemUtil.getType(itemStack);
                if (list == null || list.size() == 0) {
                    addCount(player, 1);
                } else {
                    if (list.contains(itemType)) {
                        addCount(player, 1);
                    }
                }
            }

        }
    }
}
